package com.palmgo.utils.drawer;

import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;

import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.WritableImage;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.palmcity.rtti.util.FileUtils;
import com.palmgo.utils.drawer.basic.DeformationAgient;
import com.palmgo.utils.drawer.model.HighwayTrafficBean;


/**
 * <p>TrafficThread</p>
 * <p>高速变形图自绘制线程</p>
 *
 * @author		周华彬(zhouhuabin@chinatransinfo.com, zhou_hua_bin@163.com)
 * @version		0.0.0
 * <table style="border:1px solid gray;">
 * <tr>
 * <th width="100px">版本号</th><th width="100px">动作</th><th width="100px">修改人</th><th width="100px">修改时间</th>
 * </tr>
 * <!-- 以 Table 方式书写修改历史 -->
 * <tr>
 * <td>0.0.0</td><td>创建类</td><td>zhouhuabin</td><td>2015年11月10日 下午12:04:38</td>
 * </tr>
 * <tr>
 * <td>XXX</td><td>XXX</td><td>XXX</td><td>XXX</td>
 * </tr>
 * </table>
*/
public class TrafficRenderThread implements Runnable {
	/** 变量:日志记录器,类型:Logger */
	private static Logger LOG = LoggerFactory.getLogger(TrafficRenderThread.class);
	/** 变量:渲染图片数量计数,类型:int */
	private int count = 0;
	/** 变量:实际图片渲染类,类型:DeformationAgient */
	private  DeformationAgient agient;
	/** 变量:画板类,类型:GraphicsContext */
	private GraphicsContext gc;
	/** 变量:场景,类型:Scene */
	private Scene scene;
	/** 变量:待绘制的业务数据列表,类型:List<HighwayTrafficBean> */
	private List<HighwayTrafficBean> taskList = new LinkedList<HighwayTrafficBean>();
	/**
	 * 构造器
	 * @param agient
	 * @param gc
	 * @param scene 
	 */
	public TrafficRenderThread(DeformationAgient agient, GraphicsContext gc, Scene scene) {
		super();
		this.agient = agient;
		this.gc = gc;
		this.scene = scene;
	}

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		LOG.info("图片自绘线程开始启动.taskSize="+taskList.size());
		while(true){
				count++;
				synchronized (this) {
					try {
						wait();
						redrawCanvas();
					} catch (InterruptedException e) {
						LOG.error("线程被打断异常",e);
						break;
					}
				}
		}
		
	}

	/**
	 * FIXME  void
	 */
	public void submitSegmentTrafficList(List<HighwayTrafficBean> list) {
		taskList.clear();
		taskList.addAll(list);
	}

	/**
	 * FIXME  void
	 */
	private void redrawCanvas() {
		LOG.info("图片自绘开始");
		for(final HighwayTrafficBean htb:taskList){
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				try{
					/*填充数据 */
					agient.setPngName(htb.getFileSavePath());
					agient.setData(htb.froadList, htb.rroadList);
					WritableImage writableImage = agient.convert(scene, gc);
					if(writableImage!=null){
						saveImage2File(writableImage, htb.getFileSavePath());
					}
				}catch(Exception e){
					LOG.error("绘制线程异常segmentid="+htb.getSegmentid()+",msg="+e.getLocalizedMessage());
				}
			}
		});
		}
		LOG.info("图片自绘成功结束,size=" + taskList.size());
	}
	
	/**
	 * 保存图片到指定路径，只能是png格式
	 * @param writableImage void
	 */
	public boolean saveImage2File(WritableImage writableImage, String filePath) {
		try {
			BufferedImage image = SwingFXUtils.fromFXImage(writableImage, null);
			FileUtils.makePathByFileName(filePath);
			OutputStream output = new FileOutputStream(filePath);
			ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
			ImageIO.write(image, "PNG", imageOut);
			imageOut.close();
			output.flush();
			output.close();
			return true;
		} catch (Exception e) {
			LOG.error("图片产生出现错误：" + e.getLocalizedMessage());
			return false;
		}
	}
    
}
